package com.yuanmai.fly3.demo.controllers;


import com.yuanmai.Result;
import com.yuanmai.fly3.RequestCycle;
import com.yuanmai.fly3.annotations.JSONParams;
import com.yuanmai.fly3.annotations.Path;
import com.yuanmai.fly3.annotations.PreInterceptorAnnotation;
import com.yuanmai.fly3.client.Upload;
import com.yuanmai.fly3.controller.AbstractController;
import com.yuanmai.fly3.demo.DemoPreHandlerInterceptor;
import com.yuanmai.fly3.demo.model.Address;
import com.yuanmai.fly3.demo.model.User;
import com.yuanmai.fly3.demo.services.UserService;
import com.yuanmai.fly3.internal.result.FreeMarkerResult;
import com.yuanmai.swagger2.annotation.*;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.*;
import java.io.IOException;

/**
 * Created by xux on 2017/5/2.
 */

@Api(url = "/demo",tag = "demo")
@PreInterceptorAnnotation(DemoPreHandlerInterceptor.class)
@Path("/demo")
public class DemoController extends AbstractController {

    @Inject
    @Named("server.basePath")
    private String basePath;
    @Override
    public void init() {
        System.out.println("init");
    }

    @Inject
    UserService userService;
    @GET
    @Path("/hello")
    @ApiOperation(httpMethod = "get")
    @ApiResponses(@ApiResponse(code = 200,response = String.class,message = "无意义的字符串"))
    public String hello(){
        return "say hello";
    }

    @POST @JSONParams
    @Path("/hello1")
    @ApiOperation(httpMethod = "get",url = "/hello1")
    @Params({
            @Param(name = "user",model = User.class),
            @Param(name = "address",model = Address.class)
    })
    @ApiResponses(@ApiResponse(code = 200,response = User.class,message = "用户信息"))
    public Result hello1(User user, Address address){
        System.out.println(address);
        return Result.of().data(user);
    }

    @POST
    @Path("/jaxrs1/{id}")
    public Result jaxrs1(@HeaderParam("Content-Type")String aaa,@CookieParam("JSESSIONID")String jessid,@BeanParam  User user, @FormParam("name") @DefaultValue("xxx") String name,@QueryParam("haha") @DefaultValue("默认的值") String haha,@PathParam("id") int id) throws IOException {
        Upload upload = fly().requestCycle().getClient().getUpload("file");
        logger().info("aaa:{},id:{},name:{},haha:{},fileName:{}",aaa,id,name,haha,upload.getFileName());
        return Result.of().data(user);
    }

    @ApiOperation(httpMethod = "get",url = "/hello2")
    @Params({
            @Param(name = "name",model = String.class,required = true)
    })
    @ApiResponses(@ApiResponse(code = 200,response = String.class,message = "模板"))
    @GET @Path("/hello2")
    public FreeMarkerResult hello2(RequestCycle requestCycle) throws Throwable {
        requestCycle.getModel().add("name",requestCycle.getClient().queryString("name"));

        return new FreeMarkerResult("index");
    }
    @POST @JSONParams
    @Path("/hello3")
    @ApiResponses(@ApiResponse(code = 200,response = User.class,message = "用户信息"))
    public User hello3(User user, Address address){
        System.out.println(address);
        return user;
    }
}
